package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.service.ManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("admin/product")
public class BaseManageController {

    @Autowired
    private ManageService manageService;

    // 获取一级分类数据：
    @GetMapping("getCategory1")
    public Result getCategory1(){
        // 获取服务层的数据
        List<BaseCategory1> baseCategory1List = manageService.getBaseCategory1();
        // 根据前端数据接口，返回对呀的result.data
        return Result.ok(baseCategory1List);
    }

    // 获取二级分类数据
    @GetMapping("getCategory2/{category1Id}")
    public Result getCategory2(@PathVariable Long category1Id){
        List<BaseCategory2> baseCategory2List = manageService.getBaseCategory2(category1Id);
        return Result.ok(baseCategory2List);
    }

    //获取三级分裂数据
    @GetMapping("getCategory3/{category2Id}")
    public Result getCategory3(@PathVariable Long category2Id){
        List<BaseCategory3> baseCategory3List = manageService.getBaseCategory3(category2Id);
        return Result.ok(baseCategory3List);
    }
    //根据分类id获取平台属性集合
    @GetMapping("attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result attrInfoList(@PathVariable Long category1Id,
                               @PathVariable Long category2Id,
                               @PathVariable Long category3Id){
        List<BaseAttrInfo> baseAttrInfoList = manageService.getBaseAttrInfoList(category1Id, category2Id, category3Id);
        return Result.ok(baseAttrInfoList);
    }


    // 保存平台熟悉控制器
    // http://api.gmall.com/admin/product/saveAttrInfo
    // 前端传递的参数转换成哪个实体类
    // 1. 可以自定义class实体类 2. 如果实体类中有类似的json数据接口，那我们就可以直接使用
    @PostMapping("saveAttrInfo")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo){
         manageService.saveAttrInfo(baseAttrInfo);
        return Result.ok();
    }



    // 根据平台属性Id 获取到平台属性对象，或者是平台属性值集合
    //http://api.gmall.com/admin/product/getAttrValueList/{attrId}
    @GetMapping("getAttrValueList/{attrId}")
    public Result getAttrValueList(@PathVariable Long attrId){
        //从业务角度出发，回显的是平台属性值集合，平台属性：属性值  1：n，也就是必须有平台属性才能查到平台属性值
        BaseAttrInfo baseAttrInfo = manageService.getBaseAttrInfo(attrId);
//        List<BaseAttrValue> baseAttrValueList = manageService.getAttrValueList(attrId);
//        return Result.ok(baseAttrValueList);
        return Result.ok(baseAttrInfo.getAttrValueList());
    }

}













